/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mvp.levelup.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import mvp.levelup.hashs.model.Account;

/**
 *
 * @author WORKSPOT2
 */
public class ReflectionTest
{

    public static void main(String[] args)
    {
        try
        {
            Account ac1 = new Account(1, 100500);
            ac1.setTestReflectionField("I am here!");
            Class cls = ac1.getClass();
            Field[] fields = cls.getDeclaredFields();
            Field res = null;
            for (int i = 0; i < fields.length && res == null; i++)
            {
                if (fields[i].getName().equalsIgnoreCase("testReflectionField"))
                {
                    res = fields[i];
                }
            }
            res.setAccessible(true);
            String resString = (String) res.get(ac1);
            System.out.println(resString);
            res.set(ac1, "Hacked");
            resString = (String) res.get(ac1);
            System.out.println(resString);

            Constructor<Account> constrs[] = cls.getDeclaredConstructors();
            Constructor<Account> constr = constrs[0];
            Account syntheticAccount = constr.newInstance(4,100635);
            System.out.println("Synth Account: " + syntheticAccount.toString());
            
            Method method = cls.getDeclaredMethod("secretMethod", new Class[0]);
            method.setAccessible(true);
            String res3 = (String)method.invoke(ac1, new Object[0]);
            System.out.println("res: " + res3);
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
